package com.mazegame.backend;
//
//// TextAnimation.java
//import javafx.animation.Animation;
//import javafx.animation.KeyFrame;
//import javafx.animation.Timeline;
//import javafx.scene.text.Text;
//import javafx.util.Duration;
//
//import java.util.function.Consumer;
//
//public class TextAnimation {
//    private final Text textNode;
//    private final String fullText;
//    private Timeline timeline;
//    private int currentIndex = 0;
//    private final int characterDelay; // 每个字符间隔
//    private final int textDelay;     // 动画启动延迟（毫秒）
//    private Consumer<Runnable> soundPlayCallback; // 音效播放回调
//    private Runnable onFinishedCallback; // 动画完成后的回调
//
//    public static final int DEFAULT_CHARACTER_DELAY = 100;
//    public static final int DEFAULT_TEXT_DELAY = 100;
//
//    /**
//     * 构造方法
//     *
//     * @param textNode 文本节点
//     * @param fullText 完整文本内容
//     * @param characterDelay 字符显示间隔时间（毫秒）
//     * @param textDelay 动画启动延迟（毫秒）
//     * @param soundPlayCallback 音效播放回调函数
//     */
//    public TextAnimation(Text textNode, String fullText, int characterDelay, int textDelay, Consumer<Runnable> soundPlayCallback) {
//        this.textNode = textNode;
//        this.fullText = fullText;
//        this.characterDelay = characterDelay;
//        this.textDelay = textDelay;
//        this.soundPlayCallback = soundPlayCallback;
//    }
//
//    public void start() {
//        textNode.setText("");
//        currentIndex = 0;
//
//        KeyFrame keyFrame = new KeyFrame(Duration.millis(characterDelay), event -> {
//            if (currentIndex < fullText.length()) {
//                textNode.setText(textNode.getText() + fullText.charAt(currentIndex));
//                currentIndex++;
//            } else {
//                timeline.stop();
//                if (onFinishedCallback != null) {
//                    onFinishedCallback.run();
//                }
//            }
//        });
//
//        timeline = new Timeline(keyFrame);
//        timeline.setCycleCount(Animation.INDEFINITE);
//        timeline.setDelay(Duration.millis(textDelay));
//        timeline.play();
//
//        // 开始播放打字音效
//        if (soundPlayCallback != null) {
//            soundPlayCallback.accept(this::stopSound);
//        }
//    }
//
//    // 停止当前音效
//    private void stopSound() {
//        // 这里可以传入实际音效对象进行控制
//    }
//
//    // 设置动画完成后执行的操作
//    public void setOnFinished(Runnable callback) {
//        this.onFinishedCallback = callback;
//    }
//}

import javafx.animation.Animation;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.scene.text.Text;
import javafx.util.Duration;
import javafx.scene.media.AudioClip;

public class TextAnimation {
    private final Text textNode;
    private final String fullText;
    private Timeline timeline;
    private int currentIndex = 0;
    private final int characterDelay;
    private final int textDelay;
    private AudioClip audioClip;

    public static final int DEFAULT_CHARACTER_DELAY = 100;
    public static final int DEFAULT_TEXT_DELAY = 100;

    public TextAnimation(Text textNode, String fullText, int characterDelay, int textDelay,   AudioClip audioClip) {
        this.textNode = textNode;
        this.fullText = fullText;
        this.characterDelay = characterDelay;
        this.textDelay = textDelay;
        this.audioClip = audioClip;
    }

    public void start() {
        textNode.setText("");
        currentIndex = 0;

        KeyFrame keyFrame = new KeyFrame(Duration.millis(characterDelay), event -> {
            if (currentIndex < fullText.length()) {
                textNode.setText(textNode.getText() + fullText.charAt(currentIndex));
                currentIndex++;
            } else {
                timeline.stop();
                if (audioClip != null) {
                    audioClip.stop(); // 文本显示完毕后停止音效
                }
                if (onFinishedCallback != null) {
                    onFinishedCallback.run();
                }
            }
        });

        timeline = new Timeline(keyFrame);
        timeline.setCycleCount(Animation.INDEFINITE);
        timeline.setDelay(Duration.millis(textDelay));
        timeline.play();

        if (audioClip != null) {
            audioClip.setCycleCount(AudioClip.INDEFINITE);
            audioClip.play();
        }
    }

    public void stop() {
        if (timeline != null) {
            timeline.stop();
        }

        if (audioClip != null) {
            audioClip.stop();
        }
    }

    private Runnable onFinishedCallback;

    public void setOnFinished(Runnable callback) {
        this.onFinishedCallback = callback;
    }
}

